package com.hgyc.mom.base.controller;

import java.util.*;
import jakarta.servlet.http.HttpServletResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;
import com.hgyc.mom.base.entity.Client;
import com.hgyc.mom.base.vo.ClientParam;
import com.hgyc.mom.base.service.ClientService;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.common.util.poi.ExcelUtil;

import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 客户Controller
 *
 * @author fwj
 * @since 2025-05-15
 */
@Tag(name = "客户控制器", description = "客户控制器")
@RestController
@RequestMapping("/base/client")
public class ClientController {
    @Autowired
    private ClientService clientService;

    @Operation(summary = "查询客户列表", description = "查询客户列表")
    @PostMapping("/pageList")
    public Result<IPage<Client>> pageList(@Parameter(description = "查询参数") @RequestBody ClientParam clientParam) {
        Page<Client> page = new Page<>(clientParam.getPage(), clientParam.getPageSize());
        Client client = new Client();
        BeanUtils.copyProperties(clientParam, client);
        QueryWrapper<Client> wrapper = QueryWrapperUtil.build(client);
        IPage<Client> list = clientService.page(page, wrapper);
        return Result.success(list);
    }

    @Operation(summary = "下载导入模版", description = "下载导入模版")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Client> util = new ExcelUtil<>(Client.class);
        util.importTemplateExcel(response, "客户");
    }

    @Operation(summary = "导出客户列表", description = "导出客户列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, ClientParam clientParam) {
        Client client = new Client();
        BeanUtils.copyProperties(clientParam, client);
        QueryWrapper<Client> wrapper = QueryWrapperUtil.build(client);
        List<Client> list = clientService.list(wrapper);
        ExcelUtil<Client> util = new ExcelUtil<>(Client.class);
        util.exportExcel(response, list, "客户数据");
    }

    @Operation(summary = "导入客户列表", description = "导入客户列表")
    @PostMapping("/import")
    public Result importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<Client> util = new ExcelUtil<>(Client.class);
        List<Client> clientList = util.importExcel(file.getInputStream());
        String message = clientService.importData(clientList, updateSupport);
        return Result.success(message);
    }

    @Operation(summary = "获取客户详细信息", description = "获取客户详细信息")
    @GetMapping(value = "/{id}")
    public Result getInfo(@Parameter(description = "客户ID") @PathVariable("id") Long id) {
        return Result.success(clientService.getById(id));
    }

    @Operation(summary = "新增客户", description = "新增客户")
    @PostMapping
    public Result save(@Parameter(description = "客户") @RequestBody Client client) {
        return Result.success(clientService.save(client));
    }

    @Operation(summary = "修改客户", description = "修改客户")
    @PutMapping
    public Result update(@Parameter(description = "客户") @RequestBody Client client) {
        return Result.success(clientService.updateById(client));
    }

    @Operation(summary = "删除客户", description = "删除客户")
    @DeleteMapping
    public Result remove(@Parameter(description = "删除ID") @RequestParam("ids") Long[] ids) {
        return Result.success(clientService.removeByIds(Arrays.asList(ids)));
    }
}